package com.hrzyit.base.utils

import android.content.Context
import java.text.SimpleDateFormat

/**
 */
object IMUtils {

    /**
     * 返回当前时间的格式为 yyyyMMddHHmmss
     *
     * @return
     */
    val currentTime: String
        get() {
            val sdf = SimpleDateFormat("yyyyMMddHHmmss")
            return sdf.format(System.currentTimeMillis())
        }

    /**
     * dp转dip
     *
     * @param context
     * @param dp
     * @return
     */
    fun dp2px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density + 0.5f).toInt()
    }

//    /**
//     * 文本中的emojb字符处理为表情图片
//     *
//     * @param context
//     * @param tv
//     * @param source
//     * @return
//     */
//    fun getEmotionContent(context: Context, tv: TextView, source: String): SpannableString {
//        val spannableString = SpannableString(source)
//        val res = context.resources
//
//        val regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]"
//        val patternEmotion = Pattern.compile(regexEmotion)
//        val matcherEmotion = patternEmotion.matcher(spannableString)
//
//        while (matcherEmotion.find()) {
//            // 获取匹配到的具体字符
//            val key = matcherEmotion.group()
//            // 匹配字符串的开始位置
//            val start = matcherEmotion.start()
//            // 利用表情名字获取到对应的图片
//            val imgRes = EmotionUtils.EMOTION_STATIC_MAP[key]
//            if (imgRes != null) {
//                // 压缩表情图片
//                val size = tv.textSize.toInt() * 13 / 8
//                val bitmap = BitmapFactory.decodeResource(res, imgRes!!)
//                val scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true)
//
//                val span = ImageSpan(context, scaleBitmap)
//                spannableString.setSpan(span, start, start + key.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            }
//        }
//        return spannableString
//    }

    //毫秒转秒
    fun long2String(time: Long): String {
        //毫秒转秒
        var sec = time.toInt() / 1000
        val min = sec / 60    //分钟
        sec = sec % 60        //秒
        return if (min < 10) {    //分钟补0
            if (sec < 10) {    //秒补0
                "0$min:0$sec"
            } else {
                "0$min:$sec"
            }
        } else {
            if (sec < 10) {    //秒补0
                "$min:0$sec"
            } else {
                "$min:$sec"
            }
        }
    }

    /**
     * 毫秒转化时分秒毫秒
     *
     * @param ms
     * @return
     */
    fun formatTime(ms: Long?): String {
        val ss = 1000
        val mi = ss * 60
        val hh = mi * 60
        val dd = hh * 24

        val day = ms!! / dd
        val hour = (ms - day * dd) / hh
        val minute = (ms - day * dd - hour * hh) / mi
        val second = (ms - day * dd - hour * hh - minute * mi) / ss

        val sb = StringBuffer()
        if (day > 0) {
            sb.append(day.toString() + "d")
        }
        if (hour > 0) {
            sb.append(hour.toString() + "h")
        }
        if (minute > 0) {
            sb.append("$minute′")
        }
        if (second > 0) {
            sb.append("$second″")
        }
        return sb.toString()
    }
}
